<?php

class Soul_Template_Template {

	private static $_metas = array();
    private static $_keywords = array();
    private static $_title = '';
    
    public static function setTitle($value){
    	self::$_title = $value;
    }
    
    public static function getTitle(){
    	return self::$_title;
    }
    
    public static function addKeyword($value){
    	self::$_keywords[] = $value;
    }
    
    public static function addMeta($type, $name, $content) {
        self::$_metas[] = array('type' => $type, 'value' => $name, 'content' => $content);
    }    
    
	private $_context = NULL;
	
    public function __construct() {
        $this->_context = Soul_Template_Context::getInstance();
    	$this->initialize();
    }
    
    protected function initialize(){}

    public function __get($name) {
		$result = $this->getContext()->$name;
		if(is_string($result)){
			$result = urldecode($result);
		}
        return $result;
    }
    
    public function __isset($name){
    	return $this->getContext()->hasData($name);
    }
    
    public function get($name, $default){
    	$v = $this->$name;
    	if(empty($v)){
    		return $default;
    	}
    	return $v;
    }

    public function __call($name, $arguments) {
        $args = array_merge(array($this), $arguments);
		return call_user_func_array(array($this->getContext()->getHelper(), $name), $args);
    }
    
    public function getContext(){
    	return $this->_context;
    }
    
    protected function renderMeta() {
    	Soul_Util_Array::removeEmpty(self::$_keywords);
        $result = '<meta name="keyword" content="' . implode(' ', self::$_keywords) . '"/>'."\n";
        foreach (self::$_metas as $meta) {
            $result.= "<meta {$meta['type']}=\"{$meta['value']}\" content=\"{$meta['content']}\"/>\n";
        }
        return $result;
    }

    public function render($path) {
        ob_start();
        include $path;
        $content = ob_get_clean();
        return $content;
    }
}